package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.OrderDao;

import java.sql.*;
import java.time.LocalDateTime;

public class OrderDaplmpl implements OrderDao {
    private static final String INSTERORDER_TO_ORDER = "INSERT INTO " +
            "orders (userid,orderdate,addr1,addr2,city,state,zip,country,totalprice,firstname,lastname,creditcard,cardtype,exprdate)" +
            "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    public void insertOrder(Order order) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement insertStatement = connection.prepareStatement(INSTERORDER_TO_ORDER);
            insertStatement.setString(1, order.getUsername());
            System.out.println(order.getDate());
            insertStatement.setString(2, order.getDate());
            insertStatement.setString(3, order.getAddress1());
            insertStatement.setString(4, order.getAddress2());
            insertStatement.setString(5, order.getCity());
            insertStatement.setString(6, order.getState());
            insertStatement.setString(7, order.getZip());
            insertStatement.setString(8, order.getCountry());
            insertStatement.setString(9, order.getPrice());
            insertStatement.setString(10, order.getFirstName());
            insertStatement.setString(11, order.getLastname());
            insertStatement.setString(12, order.getPhone());
            insertStatement.setString(13, order.getType());
            insertStatement.setString(14, order.getExprdate());
            insertStatement.executeUpdate();
            System.out.println("订单生成");
            insertStatement.close();
            connection.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public int getOrderID(Order order){
        int orderID = -1; // 默认值，表示未找到订单ID
        try{
            Connection connection = DBUtil.getConnection();

            // 构建 SQL 查询语句
            String sql = "SELECT orderid FROM orders WHERE "; // 假设订单表名为 "orders"

            // 根据 order 对象中的属性拼接查询条件
            if (order.getDate() != null) {
                sql += "orderdate = ? AND ";
            }
            if (order.getPhone() != null) {
                sql += "userid = ?";
            }

            int paramIndex = 1;
            PreparedStatement statement = connection.prepareStatement(sql);

            if (order.getDate() != null) {
                statement.setString(paramIndex++,order.getDate());
            }
            if (order.getUsername() != null) {
                statement.setString(paramIndex++, order.getUsername());
            }

            // 执行查询并获取结果集
            ResultSet result = statement.executeQuery();
            if (result.next()) {
                orderID = result.getInt("orderid");
            }

            // 关闭数据库连接和相关资源
            result.close();
            statement.close();
            connection.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return orderID;
    }
}
